package com.hqyj.springBoot.modules.account.dao;

import com.hqyj.springBoot.modules.account.entity.User;
import com.hqyj.springBoot.modules.common.entity.SearchBean;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface UserDao {

    @Select("select * from account_user where user_name = #{userName} and password = #{password}")
    User getUserByUserNameAndPassword(@Param("userName") String userName,@Param("password") String password);

    @Insert("insert into account_user (email, user_name, password, user_image, create_date) " +
            "values (#{email}, #{userName}, #{password}, #{userImage}, #{createDate})")
    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    void addUser(User user);

    @Update("update account_user set email = #{email}, user_name = #{userName}," +
            "password = #{password}, user_image = #{userImage} where id = #{id}")
    void updateUserByUser(User user);

    @Delete("delete from account_user where id = #{id}")
    void deleteUserById(int id);

    //链表查询将用户所带的角色也查询出来
    @Select("select * from account_user where id = #{id}")
    @Results(id="userResult", value={
            @Result(column="id", property="id"),
            @Result(column="id",property="roles",
                    javaType=List.class,
                    many=@Many(select="com.hqyj.springBoot.modules.account.dao.RoleDao.getRolesByUserId"))
    })
    User selectUserById(int id);

    @Select("<script>"
            + "select * from account_user "
            + "<where> "
            + "<if test='keyWord != \"\" and keyWord != null'>"
            + " and (email like '%${keyWord}%' or "
            + " user_name like '%${keyWord}%') "
            + "</if>"
            + "</where>"
            + "<choose>"
            + "<when test='orderBy != \"\" and orderBy != null'>"
            + " order by ${orderBy} ${direction}"
            + "</when>"
            + "<otherwise>"
            + " order by id desc"
            + "</otherwise>"
            + "</choose>"
            + "</script>")
    List<User> SelectAllUserInPage(SearchBean searchBean);

    @Select("select * from account_user where email = #{email} or user_name = #{userName}")
    List<User> getUserByUserNameOrEmail(@Param("email") String email, @Param("userName") String userName);

    @Select("select * from account_user where user_name = #{userName}")
    User selectUserByUserName(@Param("userName") String userName);

}
